#include "StdAfx.h"
#include "FileWorkingClass.h"


FileWorkingClass::FileWorkingClass(void)
{
}

FileWorkingClass::~FileWorkingClass(void)
{
}
void FileWorkingClass::ReadFromFile()
{
	CStdioFile file;
	CString line;
	if(file.Open("data\\lista.txt", CFile::modeRead|CFile::shareExclusive ))
	{
	while (file.ReadString(line))
	{
		CString temp;
		std::deque<CString> buf;
		for(int i=0;i<line.GetLength();i++)
		{
			if(line[i]==' '||line[i]=='\n')
			{
				buf.push_back(temp);
				temp.Empty();
			}
			else
				temp+=line[i];
		}
		Sprawa temp2;
		temp2.SetNumerRsow(buf[0]);
		temp2.SetNumerID(buf[1]);
		temp2.SetNumerLiczbaDziennika(buf[2]);
		temp2.SetDataZdarzenia(buf[3]);
		temp2.SetDataZgloszenia(buf[4]);
		temp2.SetDataRejestracji(buf[5]);
		temp2.SetDataZakonczenia(buf[6]);
		temp2.SetArtykul(buf[7]);
		temp2.SetStatus(buf[8]);
		m_lista.push_back(temp2);
		buf.clear();
	}
	file.Close();	
	}
}
void FileWorkingClass::SaveToFile()
{

	CStdioFile file;
	CString line;
	if(file.Open("data\\lista.txt", CFile::modeCreate|CFile::modeWrite|CFile::modeNoInherit))
	{
		while(m_lista.size()!=0)
		{
			CString temp;
			temp+=m_lista.back().GetNumerRsow();
			temp+=" ";
			temp+=m_lista.back().GetNumerID();
			temp+=" ";
			temp+=m_lista.back().GetNumerLiczbaDziennika();
			temp+=" ";
			temp+=m_lista.back().GetDataZdarzenia();
			temp+=" ";
			temp+=m_lista.back().GetDataZgloszenia();
			temp+=" ";
			temp+=m_lista.back().GetDataRejestracji();
			temp+=" ";
			temp+=m_lista.back().GetDataZakonczenia();
			temp+=" ";
			temp+=m_lista.back().GetArtykul();
			temp+=" ";
			temp+=m_lista.back().GetStatus();
			temp+=" \n";
			file.WriteString(temp);
			m_lista.pop_back();
		}
		file.Close();
	}
}
Sprawa &FileWorkingClass::AddSprawa()
{
	m_lista.resize(m_lista.size()+1);
	CString temp;
	temp.Format("RsoW%d",m_lista.size());
	m_lista.back().SetNumerRsow(temp);
	return m_lista.back();
}

Sprawa &FileWorkingClass::FindSprawa(CString _numer)
{
	for(int i=0;i<m_lista.size();i++)
	{
		if(m_lista[i].GetNumerRsow()==_numer)
			return m_lista[i];
	}	 
}
void FileWorkingClass::DeleteSprawa(CString _numer)
{
	for(int i=0;i<m_lista.size();i++)
	{
		if(m_lista[i].GetNumerRsow()==_numer)
			m_lista.erase(m_lista.begin()+i);
	}
}

void FileWorkingClass::initialization(void)
{
		CreateDirectory("data",NULL);
}


Sprawa & FileWorkingClass::operator[](int _numer)
{
		return m_lista[_numer];
}


int FileWorkingClass::GetSize(void)
{
	return m_lista.size();
}
bool FileWorkingClass::DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin = true)
{
int len = _tcslen(lpszDir);
TCHAR *pszFrom = new TCHAR[len+2];
_tcscpy(pszFrom, lpszDir);
pszFrom[len] = 0;
pszFrom[len+1] = 0;


SHFILEOPSTRUCT fileop;
fileop.hwnd	 = NULL;	 // no status display
fileop.wFunc	= FO_DELETE;	// delete operation
fileop.pFrom	= pszFrom;	 // source file name as double null terminated string
fileop.pTo	 = NULL;	 // no destination needed
fileop.fFlags	= FOF_NOCONFIRMATION|FOF_SILENT;	// do not prompt the user

if(!noRecycleBin)
fileop.fFlags |= FOF_ALLOWUNDO;

fileop.fAnyOperationsAborted = FALSE;
fileop.lpszProgressTitle	 = NULL;
fileop.hNameMappings	 = NULL;

int ret = SHFileOperation(&fileop);

delete [] pszFrom;	

return (ret == 0);
}